import fastify from 'fastify'
import { ConsulService } from './services/consule.service'
import { Container } from 'typescript-ioc'
import { IBootStrap } from './common/interfaces/bootstrap.interface'

export class Application {
  // 初始化服务
  private static readonly initializeServices = [
    ConsulService // consul服务
  ]

  // 应用初始化
  public static async initialize(server: fastify.FastifyInstance) {
    await Promise.all(
      Application.initializeServices.map(BootstrapService => {
        Container.bind(BootstrapService)
        const service = Container.get(BootstrapService)
        return service.register()
      })
    )
    return server
  }

  /**
   * 应用启动
   * @param server
   */
  public static bootstrap(server: fastify.FastifyInstance) {
    server.listen(80, '0.0.0.0').catch(err => {
      server.log.error(err)
      process.exit(1)
    })
    server.blipp()
  }
}
